<?php

class BitLy {

	private $user_name;
	private $api_key;

	function __construct($user_name, $api_key) {
		$this->user_name = $user_name;
		$this->api_key = $api_key;
	}

	function get_bitly_short_url($url, $format='txt') {
		$connectURL = 'http://api.bit.ly/v3/shorten?login=' . $this->user_name . '&apiKey=' . $this->api_key . '&uri=' . urlencode($url) . '&format=' . $format;
		
		return $this->curl_get_result($connectURL);
	}

	function get_bitly_long_url($url, $format='txt') {
		$connectURL = 'http://api.bit.ly/v3/expand?login=' . $this->user_name . '&apiKey=' . $this->api_key . '&shortUrl=' . urlencode($url) . '&format=' . $format;
		
		return $this->curl_get_result($connectURL);
	}

	function curl_get_result($url) {
		$ch = curl_init();
		$timeout = 5;
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}

	// $short_url = get_bitly_short_url('http://bekti.org/', 'shbekti', 'R_8ff98ac517f95513b3971ab9307a1940');

	// $long_url = get_bitly_long_url($short_url, 'shbekti', 'R_8ff98ac517f95513b3971ab9307a1940');
}

?>